# coding: utf-8
from PySide6.QtCore import Qt
from PySide6.QtGui import QColor
from PySide6.QtWidgets import QWidget, QVBoxLayout, QHBoxLayout
from qfluentwidgets import BodyLabel, setFont, HyperlinkButton, FluentIcon

from ...common import signalBus, HomeItemModel, BookItemModel
from ...components import HorizontalBannerView, ComicCard, HorizontalScrollArea


class ContentWidget(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.verticalLayout = QVBoxLayout(self)
        self.horizontalLayout = QHBoxLayout()
        self.titleLabel = BodyLabel("Title", self)
        self.contentLabel = BodyLabel(self)
        self.moreButton = HyperlinkButton(self)
        self.bannerScrollArea = HorizontalScrollArea(self)
        self.bannerView = QWidget(self.bannerScrollArea)
        self.bannerLayout = QHBoxLayout(self.bannerView)

        setFont(self.titleLabel, 20)
        setFont(self.contentLabel, 14)
        self.contentLabel.setTextColor(QColor(0, 0, 0, 150), QColor(255, 255, 255, 150))
        self.bannerScrollArea.setWidget(self.bannerView)

        self.verticalLayout.setAlignment(Qt.AlignmentFlag.AlignTop)
        self.horizontalLayout.setAlignment(Qt.AlignmentFlag.AlignLeft)
        self.bannerLayout.setAlignment(Qt.AlignmentFlag.AlignLeft)

        self.horizontalLayout.addWidget(self.titleLabel)
        self.horizontalLayout.addSpacing(20)
        self.horizontalLayout.addWidget(self.contentLabel, 0, Qt.AlignmentFlag.AlignBottom)
        self.horizontalLayout.addStretch(1)
        self.horizontalLayout.addWidget(self.moreButton)
        self.verticalLayout.addLayout(self.horizontalLayout)
        self.verticalLayout.addWidget(self.bannerScrollArea)
        self.setFixedHeight(320)

        self.setContent("")
        self.moreButton.setText('更多')
        self.moreButton.setIcon(FluentIcon.MORE)

    def setTitle(self, title: str):
        self.titleLabel.setText(title)

    def setContent(self, content: str):
        self.contentLabel.setText(content)
        if not content:
            self.contentLabel.hide()
        else:
            self.contentLabel.show()

    def addComicCard(self, itemModel: BookItemModel):
        card = ComicCard(self)
        card.setModel(itemModel)
        card.setFixedSize(150, 250)
        self.bannerLayout.addWidget(card, 0, Qt.AlignmentFlag.AlignLeft)

    def setModel(self, dataModel: HomeItemModel):
        self.setTitle(dataModel.title)
        self.setContent(dataModel.desc)
        if not dataModel.more_url:
            self.moreButton.hide()
        else:
            self.moreButton.setUrl(dataModel.more_url)
            self.moreButton.show()
        for comic in dataModel.comicList:
            self.addComicCard(comic)
